package com.smartrobot.smartrobot.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.media.RingtoneManager;
import android.net.Uri;

import com.loopj.android.http.SyncHttpClient;
import com.smartrobot.smartrobot.Common.Common;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.smartrobot.smartrobot.activity.MainActivity;
import com.smartrobot.smartrobot.activity.ScheduleActivity;
import com.smartrobot.smartrobot.activity.SplashActivity;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.view.ToastCustom;

/**
 * 用作封装对客户端http操作
 * <p/>
 * 而：这个操作必须卸载main线程里面否则会报错
 */
public class HttpUtil {

    static public AsyncHttpClient client = null;

    static private String url = Common.baseUrl;
    //    static private String url = "http://192.168.1.101:7070";
    static private String urltRegisterUser = url + "/user/register";
    static private String urltRegisterHairReset = url + "/user/barberRegister";
    static private String urltRegisterOrgan = url + "/user/storeRegister";
    static private String urlLogin = url + "/login_commonUse";
    static public String urlMall = url + "/mall";
    static private String urlGetActive = url + "/getDynamic";
    static private String urlGetMyNewestActive = url + "/getMyNewestDynamic";
    static private String urlSendActive = url + "/saveDynamic";
    static private String urlGetSchedule = url + "/barber/getSchedule";
    static private String urlGetOneDaySchedule = url + "/getOneDaySchedule";
    static private String urlSendSchedule = url + "/saveDaySchedule";
    static private String urlGetNews = url + "/getNews";
    static public String urlNewsDetail = url + "/getFullText?tid=";
    static public String urlGetStoreActive = url + "/getStoreActivities";
    static public String urlResetPersonalMessage = url + "/editUserMsg";
    static public String urlLogout = url + "/logout";
    static public String urlUpToHairReset = url + "/userRoleUpgrade";
    static public String urlGetStoreMessage = url + "/getStoreMsg";




    static public final int LOGIN_SUCCESS = 999;
    static public final int LOGIN_FAILD = 18;
    static public final int REGISTER_HAIRRESET_SUCCESS = 2;
    static public final int REGISTER_USER_SUCCESS = 3;
    static public final int REGISTER_FAILD = 4;
    static public final int CREATEORGAN_SUCCESS = 5;
    static public final int CREATEORGAN_FAILD = 6;
    static public final int GET_ACTIVE_SUCCESS = 7;
    static public final int GET_ACTIVE_FAILD = 8;
    static public final int SEND_ACTIVE_SUCCESS = 9;
    static public final int SEND_ACTIVE_FAILD = 10;
    static public final int GET_ACTIVE_EMPTY = 11;
    static public final int SEND_ACTIVE_PROGRESS = 12;
    static public final int GET_ACTIVE_SINGLE_SUCCESS = 13;
    static public final int GET_ACTIVE_SINGLE_FAILD = 14;
    static public final int GET_NEW = 15;
    static public final int GET_OLD = 16;
    static public final int GET_DETAIL_SCHEDULE_SUCCESS = 17;
    static public final int GET_DETAIL_SCHEDULE_FAILD = 19;
    static public final int SEND_DETAIL_SCHEDULE_SUCCESS = 20;
    static public final int SEND_DETAIL_SCHEDULE_FAILD = 21;
    static public final int GET_DETAIL_ONEDAY_SCHEDULE_SUCCESS = 22;
    static public final int GET_DETAIL_ONEDAY_SCHEDULE_FAILD = 23;
    static public final int GET_NEWS_SUCCESS = 24;
    static public final int GET_NEWS_FAILD = 25;
    static public final int GET_STORE_ACTIVE_SUCCESS = 26;
    static public final int GET_STORE_ACTIVE_FAILD = 27;
    static public final int GET_STORE_ACTIVE_FAILD_NETWORK = 28;
    static public final int RESET_PERSONAL_MESSAGE_SUCCESS = 29;
    static public final int RESET_PERSONAL_MESSAGE_FAILD = 30;
    static public final int RESET_UP_TO_HAIRRESET_SUCCESS = 31;
    static public final int RESET_UP_TO_HAIRRESET_FAILD = 32;
    static public final int GET_STORE_MESSAGE_SUCCESS = 33;
    static public final int GET_STORE_MESSAGE_FAILD = 34;


    static private final String success = "1";


    public static void init(Context context) {
        client = new AsyncHttpClient();
        client.setConnectTimeout(25000);
        client.setResponseTimeout(25000);
        PersistentCookieStore cookieStore = new PersistentCookieStore(context);
        client.setCookieStore(cookieStore);
    }


    public static void reLogin(Context contxt, Handler handler) {

        //帮助用户登录
        if (User.getInstance() != null && !User.getInstance().getId().equals("未填写")) {//先判断之前是否登录
            HttpUtil.apiLogin(contxt, new PeopleRegisterMsg(User.getInstance().getPhone(), User.getInstance().getPwd()), handler);
//            HttpUtil.apiLogin(contxt,new PeopleRegisterMsg("18925961129","123456"), handler);
            System.out.print(User.getInstance().toString());
//                        HttpUtil.apiLogin(new PeopleRegisterMsg("13828421070","123456"),handler);
        } else if (User.getUserInfo(contxt) && !User.getInstance().getId().equals("未填写")) {//之前没有登录就读取文件登录
            HttpUtil.apiLogin(contxt, new PeopleRegisterMsg(User.getInstance().getPhone(), User.getInstance().getPwd()), handler);
//            HttpUtil.apiLogin(contxt,new PeopleRegisterMsg("18925961129","123456"), handler);
            System.out.print(User.getInstance().toString());
        } else {//都没有证明用户一次都没有登录
            ToastCustom.showToast(contxt, "请登录！");
        }
    }


    public static void apiLogin(final Context context, final PeopleRegisterMsg loginMsg, final Handler handler, final int... flag) {

        RequestParams params = new RequestParams();

        params.put("userAccount", loginMsg.getPhone());
        params.put("userPassword", loginMsg.getPwd());


        client.post(urlLogin, params, new JsonHttpResponseHandler() {

            /**
             * statusCode 返回状态 heards响应头 响应服务器头 response 返回是json数据
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {

                System.out.println("成功：" + statusCode + " "
                        + response.toString());
                LogToSd.log_init();
                LogToSd.debug(response.toString());

                //登陆成功，看一下操作数据
                try {

                    if (response.getString("status").equals(success)) {

                        System.out.println("登陆,获取数据成功！");
                        User user = User.getInstance();
                        user.setId(response.getString(user.ID));
                        user.setName(response.getString(user.NAME));
                        user.setPhone(response.getString(user.PHONE));
                        user.setPwd(loginMsg.getPwd());
                        user.setHead(response.getString(user.HEAD));
                        user.setRole(response.getString(user.ROLE));
                        user.setSex(response.getString(user.SEX));
                        user.setSignature(response.getString(user.SIGNATURE));
//                        user.setQr_code(response.getString(user.QRCODE));
                        user.setAddress(response.getString(user.ADDRESS));
                        user.setNickname(response.getString(user.NICKNAME));
                        user.setAge(response.getString(user.AGE));
                        user.setBarberId(response.getString(user.BARBERID));
                        user.setBarberName(response.getString(user.BARBERNAME));

//						user.setStoreId(response.getString(user.STOREID));
//						user.setStoreName(response.getString(user.STORENAME));



                        Message msg = new Message();
                        msg.what = LOGIN_SUCCESS;

                        if (flag != null && flag.length > 0)
                            msg.arg1 = flag[0];

                        msg.obj = user.toString();
                        handler.sendMessage(msg);

                    } else {
                        System.out.println(response.getString("msg"));
                        Message msg = new Message();
                        msg.what = LOGIN_FAILD;
                        msg.obj = response.getString("status").toString();
                        handler.sendMessage(msg);
                    }

                } catch (JSONException e) {
                    //json解读失败
                    //handler 数据到主界面
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = LOGIN_FAILD;
                    msg.obj = "json解析错误!";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {


                System.out.println("失败：" + statusCode + " ");
                throwable.printStackTrace();// 打印失败
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("msg:" + throwable.toString());


                System.out.println();

                //handler 数据到主界面
                Message msg = new Message();
                msg.what = LOGIN_FAILD;
                msg.obj = "无法连接到服务器!";
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());
                Message msg = new Message();
                msg.what = LOGIN_FAILD;
                msg.obj = "无法连接到服务器!";
                handler.sendMessage(msg);

            }

        });


    }


    public static void apiLoginV2(final Context context, final PeopleRegisterMsg loginMsg, final Handler handler, final Object object, final int... flag) {

        RequestParams params = new RequestParams();

        params.put("userAccount", loginMsg.getPhone());
        params.put("userPassword", loginMsg.getPwd());


        client.post(urlLogin, params, new JsonHttpResponseHandler() {

            /**
             * statusCode 返回状态 heards响应头 响应服务器头 response 返回是json数据
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {

                System.out.println("成功：" + statusCode + " "
                        + response.toString());
                LogToSd.log_init();
                LogToSd.debug(response.toString());

                //登陆成功，看一下操作数据
                try {

                    if (response.getString("status").equals(success)) {

                        System.out.println("登陆,获取数据成功！");
                        User user = User.getInstance();
                        user.setId(response.getString(user.ID));
                        user.setName(response.getString(user.NAME));
                        user.setPhone(response.getString(user.PHONE));
                        user.setPwd(loginMsg.getPwd());
                        user.setHead(response.getString(user.HEAD));
                        user.setRole(response.getString(user.ROLE));
                        user.setSex(response.getString(user.SEX));
                        user.setSignature(response.getString(user.SIGNATURE));
//                        user.setQr_code(response.getString(user.QRCODE));
                        user.setAddress(response.getString(user.ADDRESS));
                        user.setNickname(response.getString(user.NICKNAME));
                        user.setAge(response.getString(user.AGE));
                        user.setBarberId(response.getString(user.BARBERID));
                        user.setBarberName(response.getString(user.BARBERNAME));
                        user.setIsLogin(true);//这就意味着可以跳过登录界面
//						user.setStoreId(response.getString(user.STOREID));
//						user.setStoreName(response.getString(user.STORENAME));

                        User.saveUserInfo(context);

                        Map map = new HashMap();
                        map.put("pojo",object);
                        map.put("msg","");

                        Message msg = new Message();
                        msg.what = LOGIN_SUCCESS;

                        if (flag != null && flag.length > 0)
                            msg.arg1 = flag[0];

                        msg.obj = map;
                        handler.sendMessage(msg);


                    } else {
                        System.out.println(response.getString("msg"));
                        Message msg = new Message();
                        msg.what = LOGIN_FAILD;
                        msg.obj = response.getString("msg");
                        handler.sendMessage(msg);
                    }

                } catch (JSONException e) {
                    //json解读失败
                    //handler 数据到主界面
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = LOGIN_FAILD;
                    msg.obj = "json解析错误!";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {


                System.out.println("失败：" + statusCode + " ");
                throwable.printStackTrace();// 打印失败
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("msg:" + throwable.toString());


                System.out.println();

                //handler 数据到主界面
                Message msg = new Message();
                msg.what = LOGIN_FAILD;
                msg.obj = "无法连接到服务器!";
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());
                Message msg = new Message();
                msg.what = LOGIN_FAILD;
                msg.obj = "无法连接到服务器!";
                handler.sendMessage(msg);

            }

        });


    }

    public static void apiRegisterUser(final PeopleRegisterMsg registerMsg, final Handler handler) {

        RequestParams params = new RequestParams();

        try {
            params.put(registerMsg.PHONE, registerMsg.getPhone());
            params.put(registerMsg.PWD, registerMsg.getPwd());
            params.put(registerMsg.HASHEAD, registerMsg.getHasHead());
            if (registerMsg.getHasHead().equals("1"))
                params.put(registerMsg.HEAD, new File(registerMsg.getHead()));
            else
                params.put(registerMsg.HEAD, "");

            System.out.println(params.toString());
        } catch (FileNotFoundException e) {
            System.out.println("无法找到发送文件");
            LogToSd.log_init();
            LogToSd.debug(e.getMessage());
            return;

        }


        client.post(urltRegisterUser, params, new JsonHttpResponseHandler() {

            /**
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {

                LogToSd.log_init();
                LogToSd.debug(response.toString());


                try {
                    if (response.get("status").equals(success)) {

                        Message msg = new Message();
                        msg.what = REGISTER_USER_SUCCESS;
                        msg.obj = "注册成功!";
                        handler.sendMessage(msg);

                    } else {
                        Message msg = new Message();
                        msg.what = REGISTER_FAILD;
                        msg.obj = response.get("status");
                        handler.sendMessage(msg);
                    }
                } catch (JSONException e) {

                    Message msg = new Message();
                    msg.what = REGISTER_FAILD;
                    msg.obj = "无法解析数据";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());

                Message msg = new Message();
                msg.what = REGISTER_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());
                Message msg = new Message();
                msg.what = REGISTER_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }

        });

    }

    public static void apiRegisterHairReset(PeopleRegisterMsg registerMsg, final Handler handler) {

        RequestParams params = new RequestParams();

        try {
            params.put(registerMsg.PHONE, registerMsg.getPhone());
            params.put(registerMsg.PWD, registerMsg.getPwd());
            params.put(registerMsg.HASHEAD, registerMsg.getHasHead());
            if (registerMsg.hasHead.equals("1"))
                params.put(registerMsg.HEAD, new File(registerMsg.getHead()));
            else
                params.put(registerMsg.HEAD, "");
            params.put(registerMsg.STORENAME, registerMsg.getStoreName());
            params.put(registerMsg.SMASTERACCOUNT, registerMsg.getsMasterAccount());

            System.out.println("----  " + params.toString());

        } catch (Exception e) {
            LogToSd.log_init();
            LogToSd.debug(e.getMessage());
            Message msg = new Message();
            msg.what = REGISTER_FAILD;
            msg.obj = "无法找到文件";
            handler.sendMessage(msg);
        }


        client.post(urltRegisterHairReset, params, new JsonHttpResponseHandler() {

            /**
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {


                LogToSd.log_init();
                LogToSd.debug(response.toString());
                Log.e("register hairreset ======>",response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        Message msg = new Message();
                        msg.what = REGISTER_HAIRRESET_SUCCESS;
                        msg.obj = "注册成功";
                        handler.sendMessage(msg);


                    } else {
                        Message msg = new Message();
                        msg.what = REGISTER_FAILD;
                        msg.obj = response.get("status");
                        handler.sendMessage(msg);
                    }
                } catch (JSONException e) {

                    Message msg = new Message();
                    msg.what = REGISTER_FAILD;
                    msg.obj = "无法找到json文件";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());

                Message msg = new Message();
                msg.what = REGISTER_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());
                Message msg = new Message();
                msg.what = REGISTER_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }

        });

    }

    public static void apiRegisterOrgan(Organ organ, final Handler handler) {

        RequestParams params = new RequestParams();

        try {
            params.put(organ.MASTERID, organ.getMasterId());
            params.put(organ.MASTERACCOUNT, organ.getMasterAccount());
            params.put(organ.NAME, organ.getName());
            params.put(organ.BUSINESSLIENSE, new File(organ.getBusinessLiense()));
            params.put(organ.IDENTIFYCARD, new File(organ.getIdentifyCard()));
            params.put(organ.PROVINCE, organ.getProvince());
            params.put(organ.CITY, organ.getCity());
            params.put(organ.AREA, organ.getArea());
            params.put(organ.ADDRESS, organ.getAddress());
            params.put(organ.POSTCODE, organ.getPostCode());
            params.put(organ.REGISTTYPE, organ.registType);


            System.out.println("parmas --->" + params.toString());


        } catch (FileNotFoundException e) {
            Message msg = new Message();
            msg.what = CREATEORGAN_FAILD;
            msg.obj = "无法找到文件";
            handler.sendMessage(msg);
            return;

        }

        client.post(urltRegisterOrgan, params, new JsonHttpResponseHandler() {

            /**
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {

                LogToSd.log_init();
                LogToSd.debug(response.toString());
                Log.e("============>",response.toString());
                try {
                    if (response.get("status").equals(success)) {

                        Message msg = new Message();
                        msg.what = CREATEORGAN_SUCCESS;
                        msg.obj = "注册店铺成功";
                        handler.sendMessage(msg);

                    } else {

                        Message msg = new Message();
                        msg.what = CREATEORGAN_FAILD;
                        msg.obj = response.get("status").toString();
                        handler.sendMessage(msg);

                    }
                } catch (JSONException e) {
                    Message msg = new Message();
                    msg.what = CREATEORGAN_FAILD;
                    msg.obj = "解析错误";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());
                Message msg = new Message();
                msg.what = CREATEORGAN_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(throwable.toString());
                Message msg = new Message();
                msg.what = CREATEORGAN_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }
        });


    }


    public static void apiGetActive(final Map<String, String> getActiveMap, final List<Active> activeList, final Handler handler) {

        RequestParams params = new RequestParams();

        params.put("lasttime", getActiveMap.get("lasttime"));
        params.put("num", getActiveMap.get("num"));
        params.put("pos", getActiveMap.get("pos"));//获取那边的动态
        params.put("way", getActiveMap.get("way"));

        client.post(urlGetActive, params, new JsonHttpResponseHandler() {

            /**
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                try {
                    LogToSd.log_init();
                    LogToSd.debug(statusCode + ":" + response.toString());

                    if (response.getString("status").equals(success)) {
                        JSONArray res = response.getJSONArray("res");
                        System.out.println("res" + res.toString());
                        if (res != null) {
                            for (int i = 0; i < res.length(); i++) {

                                try {

                                    JSONObject tempJson = res.getJSONObject(i);
                                    Active active = new Active();
                                    active.setuId(tempJson.getString(Active.UID));
                                    active.settId(tempJson.getString(Active.TID));
                                    active.setuHead(tempJson.getString(Active.UHEAD));
                                    active.setuNickName(tempJson.getString(Active.UNICKNAME));
                                    active.setuAccount(tempJson.getString(Active.UACCOUNT));
                                    active.setuHead(tempJson.getString(Active.UHEAD));
                                    active.setContent(tempJson.getString(Active.CONTENT));
                                    active.setTime(tempJson.getString(Active.TIME));
                                    active.setShareType(tempJson.getString(Active.SHARETYPE));
                                    active.setFileUrl(tempJson.getString(Active.FILEURL));
                                    active.setSharePos(tempJson.getString(Active.SHAREPOS));
                                    active.setPraiseNum(tempJson.getString(Active.PRASENUM));
                                    active.setTimeResort(tempJson.getString(Active.TIMERESORT));


                                    activeList.add(active);
                                } catch (Exception e) {
                                    Message msg = new Message();
                                    msg.what = GET_ACTIVE_FAILD;
                                    msg.obj = "解析错误";
                                    msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                                    handler.sendMessage(msg);


                                }

                            }

                            Message msg = new Message();
                            msg.what = GET_ACTIVE_SUCCESS;
                            //判断返回数据时最新数据还是阅读以前的数据
                            if (getActiveMap.get("way").equals("new"))
                                msg.arg1 = GET_NEW;
                            else
                                msg.arg1 = GET_OLD;
                            msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                            if(getActiveMap.get("size") != null) msg.arg2 = 3;
                            msg.obj = response.get("msg");
                            handler.sendMessage(msg);


                        } else {

                            Message msg = new Message();
                            msg.what = GET_ACTIVE_FAILD;
                            msg.obj = response.get("msg");
                            msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                            handler.sendMessage(msg);

                        }

                    } else {
                        Message msg = new Message();
                        msg.what = GET_ACTIVE_FAILD;
                        msg.obj = response.getString("status");
                        msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                        handler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    Message msg = new Message();
                    msg.what = GET_ACTIVE_EMPTY;
                    msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                    msg.obj = "没有新数据";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("错误：" + statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_ACTIVE_FAILD;
                msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("错误：" + statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_ACTIVE_FAILD;
                msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);
                Log.d("------->", "失败了2");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("错误：" + statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_ACTIVE_FAILD;
                msg.arg2 = Integer.valueOf(getActiveMap.get("pos"));
                msg.obj = "无法连接服务器";
                Log.d("------->", "失败了3");
                handler.sendMessage(msg);

            }


        });
    }


    public static void apiSendActive(Map<String, String> sendMap, final Handler handler) {

        RequestParams params = new RequestParams();


        try {
            params.put("content", sendMap.get("content"));
            params.put("sharePos", sendMap.get("sharePos"));
            params.put("shareType", sendMap.get("shareType"));
            if (sendMap.get("shareType").equals("图片"))
                params.put("file", new File(sendMap.get("file")));
            else
                params.put("file", "");

            Log.e("-------------->", params.toString());
        } catch (FileNotFoundException e) {
            Message msg = new Message();
            msg.what = SEND_ACTIVE_FAILD;
            msg.obj = "没有找到发送图片";
            handler.sendMessage(msg);
        }


        client.post(urlSendActive, params, new JsonHttpResponseHandler() {

            @Override
            public void onProgress(int bytesWritten, int totalSize) {

                Message msg = new Message();
                msg.what = SEND_ACTIVE_PROGRESS;
                msg.arg1 = (int) ((bytesWritten * 1.0f / totalSize * 1.0f) * 100);//整形会隐藏百分号
                handler.sendMessage(msg);


            }

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {

                LogToSd.log_init();
                LogToSd.debug("成功：" + statusCode + ":" + response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        Message msg = new Message();
                        msg.what = SEND_ACTIVE_SUCCESS;
                        msg.obj = response.get("msg");
                        handler.sendMessage(msg);


                    } else {

                        Message msg = new Message();
                        msg.what = SEND_ACTIVE_FAILD;
                        msg.obj = response.get("msg");
                        handler.sendMessage(msg);

                    }
                } catch (JSONException e) {
                    Message msg = new Message();
                    msg.what = SEND_ACTIVE_FAILD;
                    msg.obj = "解析错误";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("错误：" + statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = SEND_ACTIVE_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug("错误：" + statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = SEND_ACTIVE_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }
        });


    }


    public static void apiGetSingleActive(final Map<String, String> getActiveMap, final List<Active> list, final Handler handler) {


        RequestParams params = new RequestParams();

        params.put("pos", getActiveMap.get("pos"));//获取那边的动态


        client.post(urlGetMyNewestActive, params, new JsonHttpResponseHandler() {

            /**
             */

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {

                if (response != null)
                    LogToSd.debug(statusCode + ":" + response.toString());

                try {
                    if (response.getString("status").equals(success)) {
                        JSONArray root = response.getJSONArray("res");
                        JSONObject tempJson = root.getJSONObject(0);

                        if (tempJson != null) {

                            try {
                                Active active = new Active();
                                active.setuId(tempJson.getString(Active.UID));
                                active.settId(tempJson.getString(Active.TID));
                                active.setuAccount(tempJson.getString(Active.UACCOUNT));
                                active.setuHead(tempJson.getString(Active.UHEAD));
                                active.setuNickName(tempJson.getString(Active.UNICKNAME));
                                active.setuHead(tempJson.getString(Active.UHEAD));
                                active.setContent(tempJson.getString(Active.CONTENT));
                                active.setTime(tempJson.getString(Active.TIME));
                                active.setShareType(tempJson.getString(Active.SHARETYPE));
                                active.setFileUrl(tempJson.getString(Active.FILEURL));
                                active.setSharePos(tempJson.getString(Active.SHAREPOS));
                                active.setPraiseNum(tempJson.getString(Active.PRASENUM));
                                active.setTimeResort(tempJson.getString(Active.TIMERESORT));

                                list.add(active);
                                Message msg = new Message();
                                msg.what = GET_ACTIVE_SINGLE_SUCCESS;
                                msg.arg2 = Integer.valueOf(getActiveMap.get("size"));
                                msg.obj = active;
                                handler.sendMessage(msg);

                            } catch (Exception e) {
                                Message msg = new Message();
                                msg.what = GET_ACTIVE_SINGLE_FAILD;
                                msg.obj = "解析错误";
                                handler.sendMessage(msg);

                            }


                        } else {

                            Message msg = new Message();
                            msg.what = GET_ACTIVE_SINGLE_FAILD;
                            msg.obj = response.get("msg");
                            handler.sendMessage(msg);

                        }

                    } else {
                        Message msg = new Message();
                        msg.what = GET_ACTIVE_SINGLE_FAILD;
                        msg.obj = response.getString("status");
                        handler.sendMessage(msg);
                    }

                } catch (JSONException e) {
                    Message msg = new Message();
                    msg.what = GET_ACTIVE_SINGLE_FAILD;
                    msg.obj = "没有新数据";
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {

                throwable.printStackTrace();
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_ACTIVE_SINGLE_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.debug(statusCode + ":" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_ACTIVE_SINGLE_FAILD;
                msg.obj = "无法连接服务器";
                handler.sendMessage(msg);

            }


        });

    }


    public static void apiGetSchedule(final Map<String, String> getScheduleMap, final List<Schedule> todayList,
                                      final List<Schedule> tomorrowList, final List<Schedule> nextTomorrowList, final Handler handler) {

        LogToSd.log_init();
        RequestParams params = new RequestParams();

        client.post(urlGetSchedule, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("获取日程：" + response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        JSONArray tempJson = null;
                        JSONObject jsonObj = null;
                        JSONObject root = response.getJSONObject("res");

                        tempJson = root.getJSONArray("Today_schedule");
                        for (int i = 0; i < tempJson.length(); i++) {//解析每一条json出来
                            jsonObj = tempJson.getJSONObject(i);
                            if (!jsonObj.getString(Schedule.ID).equals("")) {//不读取初始化数据

                                Schedule schedule = new Schedule();
                                schedule.setId(jsonObj.getString(schedule.ID));
                                schedule.setTime(jsonObj.getString(schedule.TIME));
                                schedule.setEvent(jsonObj.getString(schedule.EVEN));
                                schedule.setDate(jsonObj.getString(schedule.DATE));

                                Log.d("今天里面的对象--------", schedule.toString());
                                todayList.add(schedule);
                            }


                        }

                        Log.d("今天--------", tempJson.toString());

                        tempJson = root.getJSONArray("Tomorrow_schedule");
                        for (int i = 0; i < tempJson.length(); i++) {
                            jsonObj = tempJson.getJSONObject(i);
                            if (!jsonObj.getString(Schedule.ID).equals("")) {

                                Schedule schedule = new Schedule();
                                schedule.setId(jsonObj.getString(schedule.ID));
                                schedule.setTime(jsonObj.getString(schedule.TIME));
                                schedule.setEvent(jsonObj.getString(schedule.EVEN));
                                schedule.setDate(jsonObj.getString(schedule.DATE));

                                Log.d("明天里面的对象-------", schedule.toString() + schedule.getId());
                                tomorrowList.add(schedule);
                            }
                        }
                        Log.d("明天--------", tempJson.toString());

                        tempJson = root.getJSONArray("AfterDay_schedule");
                        for (int i = 0; i < tempJson.length(); i++) {
                            jsonObj = tempJson.getJSONObject(i);
                            if (!jsonObj.getString(Schedule.ID).equals("")) {


                                Schedule schedule = new Schedule();
                                schedule.setId(jsonObj.getString(schedule.ID));
                                schedule.setTime(jsonObj.getString(schedule.TIME));
                                schedule.setEvent(jsonObj.getString(schedule.EVEN));
                                schedule.setDate(jsonObj.getString(schedule.DATE));

                                Log.d("后天里面的对象--------", schedule.toString());
                                nextTomorrowList.add(schedule);
                            }
                        }
                        Log.d("后天--------", tempJson.toString());

                        Message msg = new Message();
                        msg.what = GET_DETAIL_SCHEDULE_SUCCESS;
                        handler.sendMessage(msg);


                    } else {
                        //连接服务器成功，某些原因失败
                        Message msg = new Message();
                        msg.what = GET_DETAIL_SCHEDULE_FAILD;
                        msg.obj = response.getString("status");
                        handler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = GET_DETAIL_SCHEDULE_FAILD;
                    handler.sendMessage(msg);
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取日程失败" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_DETAIL_SCHEDULE_FAILD;
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取日程失败" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_DETAIL_SCHEDULE_FAILD;
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取日程失败" + throwable.toString());

                Message msg = new Message();
                msg.what = GET_DETAIL_SCHEDULE_FAILD;
                handler.sendMessage(msg);

            }
        });
    }

    public static void apiGetOneDaySchedule(final Map<String, String> map, final List<Schedule> list, final Handler handler) {

        LogToSd.log_init();
        RequestParams params = new RequestParams();
        params.put("type", map.get("type"));

        try {
            client.post(urlGetOneDaySchedule, params, new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {


                    //写入日志
                    LogToSd.log_init();
                    LogToSd.debug("获取单天日程：" + response.toString());


                    try {
                        if (response.get("status").equals(success)) {

                            JSONArray root;
                            root = response.getJSONArray("res");
                            for (int i = 0; i < root.length(); i++) {

                                JSONObject temp = root.getJSONObject(i);
                                if (!temp.getString(Schedule.ID).equals("")) {//不读取初始化数据

                                    Schedule schedule = new Schedule();
                                    schedule.setId(temp.getString(schedule.ID));
                                    schedule.setTime(temp.getString(schedule.TIME));
                                    schedule.setEvent(temp.getString(schedule.EVEN));
                                    schedule.setDate(temp.getString(schedule.DATE));

                                    Log.d("一天里面的对象--------", schedule.toString());
                                    list.add(schedule);
                                }


                            }

                            Message msg = new Message();
                            msg.what = GET_DETAIL_ONEDAY_SCHEDULE_SUCCESS;
                            msg.obj = map.get("type");//返回通知哪一个list
                            handler.sendMessage(msg);


                        } else {
                            //连接服务器成功，某些原因失败

                            Message msg = new Message();
                            msg.what = GET_DETAIL_ONEDAY_SCHEDULE_FAILD;
                            msg.obj = response.get("status");
                            handler.sendMessage(msg);


                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        LogToSd.log_init();
                        LogToSd.debug("获取单天日程Json解析错误：" + e.toString());
                        Message msg = new Message();
                        msg.what = GET_DETAIL_ONEDAY_SCHEDULE_FAILD;
                        handler.sendMessage(msg);
                    }


                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                    LogToSd.log_init();
                    if (throwable != null)
                        LogToSd.error("获取日程失败" + throwable.toString());
                    Message msg = new Message();
                    msg.what = GET_DETAIL_ONEDAY_SCHEDULE_FAILD;
                    handler.sendMessage(msg);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    LogToSd.log_init();
                    if (throwable != null)
                        LogToSd.error("获取日程失败" + throwable.toString());
                    Message msg = new Message();
                    msg.what = GET_DETAIL_ONEDAY_SCHEDULE_FAILD;
                    handler.sendMessage(msg);
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    LogToSd.log_init();
                    if (throwable != null)
                        LogToSd.error("获取日程失败" + throwable.toString());
                    Message msg = new Message();
                    msg.what = GET_DETAIL_ONEDAY_SCHEDULE_FAILD;
                    handler.sendMessage(msg);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void apiGetOneDayScheduleToFile(final Context context, final String type) {

        final List<Schedule> list = new ArrayList<Schedule>();


        LogToSd.log_init();
        RequestParams params = new RequestParams();
        params.put("type", type);
        AsyncHttpClient aClient = new SyncHttpClient();
        PersistentCookieStore cookieStore = new PersistentCookieStore(context);
        aClient.setCookieStore(cookieStore);

        aClient.post(urlGetOneDaySchedule, params, new JsonHttpResponseHandler() {



            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("获取单天日程：" + response.toString());
                String fileName = "";
                try {

                    if (type.equals("0"))
                        fileName = FileUtil.detailCachePath
                                + User.getInstance().getId() + User.getInstance().getPhone() + TimeUtil.getTodayData()
                                + FileUtil.detailEndName;
                    if (type.equals("1"))
                        fileName = FileUtil.detailCachePath
                                + User.getInstance().getId() + User.getInstance().getPhone() + TimeUtil.getTomoData()
                                + FileUtil.detailEndName;
                    if (type.equals("2"))
                        fileName = FileUtil.detailCachePath
                                + User.getInstance().getId() + User.getInstance().getPhone() + TimeUtil.getTheDayData()
                                + FileUtil.detailEndName;


                    if (response.get("status").equals(success)) {

                        JSONArray root;
                        root = response.getJSONArray("res");
                        for (int i = 0; i < root.length(); i++) {

                            JSONObject temp = root.getJSONObject(i);
                            if (!temp.getString(Schedule.ID).equals("")) {//不读取初始化数据

                                Schedule schedule = new Schedule();
                                schedule.setId(temp.getString(schedule.ID));
                                schedule.setTime(temp.getString(schedule.TIME));
                                schedule.setEvent(temp.getString(schedule.EVEN));
                                schedule.setDate(temp.getString(schedule.DATE));

                                Log.d("一天里面的对象--------", schedule.toString());
                                list.add(schedule);
                            }


                        }

                        //写到文件中
                        FileUtil.writeScheduleToFile(list, fileName);

                        //通知您的理发师更新了日程
                        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                                .setSmallIcon(context.getApplicationInfo().icon)
                                .setWhen(System.currentTimeMillis()).setAutoCancel(true);

                        //设置状态栏提示
                        mBuilder.setTicker("您的专属理发师更改了日程!");
                        mBuilder.setContentTitle("日程变更通知");
                        mBuilder.setContentText("您的专属理发师日程改变了!");


                        //必须设置pendingintent，否则在2.3的机器上会有bug
                        Intent intent ;
                        if(MainActivity.instance != null)
                            intent = new Intent(context, MainActivity.class);
                        else
                            intent = new Intent(context, SplashActivity.class);
                        intent.putExtra("notiId", "" + GET_DETAIL_ONEDAY_SCHEDULE_SUCCESS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        PendingIntent pendingIntent = PendingIntent.getActivity(context, GET_DETAIL_ONEDAY_SCHEDULE_SUCCESS, intent, PendingIntent.FLAG_ONE_SHOT);
                        mBuilder.setContentIntent(pendingIntent);

                        //设置声音
                        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        mBuilder.setSound(alarmSound);

                        Notification notification = mBuilder.build();
                        notificationManager.notify(GET_DETAIL_ONEDAY_SCHEDULE_SUCCESS, notification);



                        if (ScheduleActivity.scheduleInstance != null) {//若是不为空这就意味着还在那个页面

                            //更新数据
                            ScheduleActivity.scheduleInstance.NotiServerDataChange();

                        }


                    } else {
                        //连接服务器成功，某些原因失败
                        LogToSd.log_init();
                        LogToSd.debug("返回其他错误");

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    LogToSd.log_init();
                    LogToSd.debug("获取单天日程Json解析错误：" + e.toString());
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取日程失败" + throwable.toString());
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取日程失败" + throwable.toString());
            }


            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取日程失败" + throwable.toString());
            }
        });
    }

    public static void apiSendSchedule(final Map<String, String> map, final List<Schedule> list, final Handler handler) {

        LogToSd.log_init();
        RequestParams params = new RequestParams();
        params.put("type", map.get("type"));
        params.put("matter", map.get("matter"));
        params.put("time", map.get("time"));

        client.post(urlSendSchedule, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("发送日程：" + response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        //获取成功解析json
                        Message msg = new Message();
                        msg.what = SEND_DETAIL_SCHEDULE_SUCCESS;
                        msg.obj = map.get("type");
                        handler.sendMessage(msg);


                    } else {
                        //连接服务器成功，某些原因失败
                        Message msg = new Message();
                        msg.what = SEND_DETAIL_SCHEDULE_FAILD;
                        msg.obj = response.get("status");
                        handler.sendMessage(msg);

                    }
                } catch (JSONException e) {
                    //获取status这个json错误
                    LogToSd.log_init();
                    LogToSd.error("发送日程Json解析错误" + e.toString());
                    Message msg = new Message();
                    msg.what = SEND_DETAIL_SCHEDULE_FAILD;
                    handler.sendMessage(msg);

                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("发送日程失败" + throwable.toString());
                Message msg = new Message();
                msg.what = SEND_DETAIL_SCHEDULE_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("发送日程失败" + statusCode + throwable.toString());
                Message msg = new Message();
                msg.what = SEND_DETAIL_SCHEDULE_FAILD;
                handler.sendMessage(msg);
            }


            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("发送日程失败" + throwable.toString());
                Message msg = new Message();
                msg.what = SEND_DETAIL_SCHEDULE_FAILD;
                handler.sendMessage(msg);
            }
        });
    }


    public static void apiGetNews(final Map<String, String> map, final List<News> list, final Handler handler) {

        LogToSd.log_init();
        final RequestParams params = new RequestParams();
        params.put("classify", map.get("classify"));
        params.put("lastTime", map.get("lastTime"));
        params.put("num", map.get("num"));
        params.put("way", map.get("way"));


        client.post(urlGetNews, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.error("获取资讯：" + response.toString());
                Log.e("------prams-------->", params.toString());
                Log.e("------获取资讯-------->", response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        JSONArray root = response.getJSONArray("res");
                        JSONObject temp;
                        for (int i = 0; i < root.length(); i++) {

                            temp = root.getJSONObject(i);
                            News news = new News();
                            news.setId(temp.getString(news.ID));
                            news.setTitle(temp.getString(news.TITLE));
                            news.setSimpleContent(temp.getString(news.SIMPLECONTENT));
                            news.setFileType(temp.getString(news.FILETYPE));
                            news.setSimpleImg(temp.getString(news.SIMPLEIMG));
                            news.setTime(temp.getString(news.TIME));
                            news.setTimeSort(temp.getString(news.TIMESORT));

                            list.add(news);

                        }


                        Message msg = new Message();
                        msg.what = GET_NEWS_SUCCESS;
                        msg.obj = map.get("way");
                        handler.sendMessage(msg);

                    } else {
                        //连接服务器成功，某些原因失败

                        Message msg = new Message();
                        msg.what = GET_NEWS_FAILD;
                        if ("up".equals(map.get("way")))
                            msg.arg1 = 0;
                        else
                            msg.arg1 = 1;
                        msg.obj = response.get("status");
                        handler.sendMessage(msg);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    LogToSd.log_init();
                    LogToSd.debug("获取资讯json解析错误：" + e.toString());

                    Message msg = new Message();
                    msg.what = GET_NEWS_FAILD;
                    handler.sendMessage(msg);

                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取资讯错误：" + throwable.toString());
                Message msg = new Message();
                msg.what = GET_NEWS_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取资讯错误：" + throwable.toString());
                Message msg = new Message();
                msg.what = GET_NEWS_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if (throwable != null)
                    LogToSd.error("获取资讯错误：" + throwable.toString());
                Message msg = new Message();
                msg.what = GET_NEWS_FAILD;
                handler.sendMessage(msg);
            }
        });
    }


    public static void apiGetStoreActive(final Map<String, String> map, final List<StoreActive> list, final Handler handler) {

        LogToSd.log_init();
        RequestParams params = new RequestParams();
        params.put("num",map.get("num"));
        params.put("way",map.get("way"));
        params.put("lastTime",map.get("lastTime"));
        Log.e("========>",params.toString());

        client.post(urlGetStoreActive, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("获取店内活动：" + response.toString());
                Log.e("========>",response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        JSONArray root = response.getJSONArray("res");
                        JSONObject obj = null;
                        for(int i=0;i<root.length();i++){
                            obj = root.getJSONObject(i);
                            StoreActive active = new StoreActive();
                            active.setId(obj.getString(active.ID));
                            active.setName(obj.getString(active.NAME));
                            active.setTitle(obj.getString(active.TITLE));
                            active.setSynopsis(obj.getString(active.SYNOPSIS));
                            active.setTime(obj.getString(active.TIME));
                            active.setValidity(obj.getString(active.VALIDITY));
                            active.setTimeSign(obj.getString(active.TIMESIGN));
                            active.setUrl(obj.getString(active.URL));

                            //添加到list里面去
                            list.add(active);



                        }

                        root = null;
                        obj = null;
                        Message msg = new Message();
                        msg.what = GET_STORE_ACTIVE_SUCCESS;
                        msg.obj = map.get("way");
                        handler.sendMessage(msg);



                    } else {
                        //连接服务器成功，某些原因失败
                        Message msg = new Message();
                        msg.what = GET_STORE_ACTIVE_FAILD;
                        if(map.get("way").equals("up"))
                            msg.arg1 = 0;
                        else
                            msg.arg1 = 1;
                        msg.obj = response.get("status").toString();
                        handler.sendMessage(msg);


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = GET_STORE_ACTIVE_FAILD;
                    handler.sendMessage(msg);

                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {

                LogToSd.log_init();
                if(throwable != null)
                LogToSd.error("获取店内活动失败" + throwable.getMessage().toString());
                Message msg = new Message();
                msg.what = GET_STORE_ACTIVE_FAILD_NETWORK;
                handler.sendMessage(msg);



            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if(throwable != null)
                LogToSd.error("获取店内活动失败" + throwable.toString());
                Message msg = new Message();
                msg.what = GET_STORE_ACTIVE_FAILD_NETWORK;
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                LogToSd.error("获取店内活动失败" + throwable.toString());
                Message msg = new Message();
                msg.what = GET_STORE_ACTIVE_FAILD_NETWORK;
                handler.sendMessage(msg);


            }
        });
    }




    public static void apiResetPersonMessage(final Map<String, String> map,  final Handler handler) {

        LogToSd.log_init();
        RequestParams params = new RequestParams();
        params.put("key",map.get("key"));
        params.put("value",map.get("value"));

        if(map.get("key").equals("2"))
            try {
                Log.d("pic path ==============>",map.get("file"));
                params.put("file",new File(map.get("file")));
            } catch (Exception e) {
                e.printStackTrace();
                Message msg = new Message();
                msg.what = RESET_PERSONAL_MESSAGE_FAILD;
                msg.obj = "无法找到文件";
                handler.sendMessage(msg);


            }
        else
            params.put("file", map.get("file"));


        client.post(urlResetPersonalMessage, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.error("修改个人信息：" + response.toString());
                Log.d("==================>",response.toString());

                try {
                    if (response.get("status").equals(success)) {

                        Message msg = new Message();
                        msg.what = RESET_PERSONAL_MESSAGE_SUCCESS;
                        msg.arg1 = Integer.valueOf(map.get("key"));
                        msg.obj = map.get("value");
                        handler.sendMessage(msg);

                    } else {
                        //连接服务器成功，某些原因失败
                        Message msg = new Message();
                        msg.what = RESET_PERSONAL_MESSAGE_FAILD;
                        msg.obj = response.get("status");
                        handler.sendMessage(msg);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = RESET_PERSONAL_MESSAGE_FAILD;
                    handler.sendMessage(msg);
                }

            }


            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("修改个人信息失败" + throwable.getMessage().toString());
                Message msg = new Message();
                msg.what = RESET_PERSONAL_MESSAGE_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("修改个人信息失败" + throwable.toString());
                Message msg = new Message();
                msg.what = RESET_PERSONAL_MESSAGE_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("修改个人信息失败" + throwable.toString());
                Message msg = new Message();
                msg.what = RESET_PERSONAL_MESSAGE_FAILD;
                handler.sendMessage(msg);
            }
        });
    }


    public static void apiUpToHariReset(final Map<String,String> map,final Handler handler) {


        RequestParams params = new RequestParams();
        params.put("storeName",map.get("storeName"));
        params.put("storekeeperAccount",map.get("storekeeperAccount"));


        client.post(urlUpToHairReset, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("升级理发师："+response.toString());
                Log.e("=====================>",response.toString());

    try {
        if(response.get("status").equals(success)){

           Message msg = new Message();
            msg.what = RESET_UP_TO_HAIRRESET_SUCCESS;
            handler.sendMessage(msg);

        }else{
            //连接服务器成功，某些原因失败
            Message msg = new Message();
            msg.what = RESET_UP_TO_HAIRRESET_FAILD;
            msg.obj = response.get("status").toString();
            handler.sendMessage(msg);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        Message msg = new Message();
        msg.what = RESET_UP_TO_HAIRRESET_FAILD;
        handler.sendMessage(msg);
    }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                LogToSd.error("升级理发师失败"+throwable.toString());
                Message msg = new Message();
                msg.what = RESET_UP_TO_HAIRRESET_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("升级理发师失败"+throwable.toString());
                Message msg = new Message();
                msg.what = RESET_UP_TO_HAIRRESET_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("升级理发师失败"+throwable.toString());
                Message msg = new Message();
                msg.what = RESET_UP_TO_HAIRRESET_FAILD;
                handler.sendMessage(msg);
            }
        });
}


    public static void apiFindOrganMessage(final Map<String,String> map ,final Handler handler) {


        RequestParams params = new RequestParams();
        params.put("storeName",map.get("storeName"));
        params.put("storekeeperAccount",map.get("storekeeperAccount"));


        client.post(urlGetStoreMessage, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("查找店铺信息："+response.toString());
                Log.e("========查找店铺信息==========>",response.toString());

                try {
                    if(response.get("status").equals(success)){

                        Map<String,String> map = new HashMap<String, String>();

                        JSONObject res = response.getJSONObject("res");

                        map.put("nickName",res.get("s_shopkeeper_nickname").toString());
                        map.put("name",res.get("s_shopkeeper_name").toString());
                        map.put("head",res.get("s_shopkeeper_head").toString());
                        map.put("address", res.get("s_address").toString());

                        Message msg = new Message();
                        msg.what = GET_STORE_MESSAGE_SUCCESS;
                        msg.obj = map;
                        handler.sendMessage(msg);

                    }else{
                        //连接服务器成功，某些原因失败
                        Message msg = new Message();
                        msg.what = GET_STORE_MESSAGE_FAILD;
                        msg.obj = response.get("status").toString();
                        handler.sendMessage(msg);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = GET_STORE_MESSAGE_FAILD;
                    handler.sendMessage(msg);
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("查找店铺信息失败"+throwable.toString());
                Message msg = new Message();
                msg.what = GET_STORE_MESSAGE_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("查找店铺信息失败"+throwable.toString());
                Message msg = new Message();
                msg.what = GET_STORE_MESSAGE_FAILD;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("查找店铺信息失败"+throwable.toString());
                Message msg = new Message();
                msg.what = GET_STORE_MESSAGE_FAILD;
                handler.sendMessage(msg);
            }
        });
    }


    public static void apiLogout() {

        RequestParams params = new RequestParams();

        client.post(urlLogout, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                //写入日志
                LogToSd.log_init();
                LogToSd.debug("注销信息："+response.toString());
                Log.e("=============>",response.toString());

                try {
                    if(response.get("status").equals(success)){



                    }else{
                        //连接服务器成功，某些原因失败

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("注销信息失败"+throwable.toString());
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("注销信息失败"+throwable.toString());
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                LogToSd.log_init();
                if(throwable != null)
                    LogToSd.error("注销信息失败"+throwable.toString());
            }
        });
    }


// public static void apiGetSchedule(final Map<String,String> getActiveMap, final List<Active> list,final Handler handler) {
//
//        LogToSd.log_init();
//        RequestParams params = new RequestParams();
//
//        client.post(urlSendActive, params, new JsonHttpResponseHandler() {
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//
//                //写入日志
//    LogToSd.log_init();
//                LogToSd.debug("获取日程："+response.toString());

//    try {
//        if(response.get("status").equals(success)){
//
//        }else{
//            //连接服务器成功，某些原因失败
//
//        }
//    } catch (JSONException e) {
//        e.printStackTrace();
//    }
//
//
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
//    LogToSd.log_init();
//                LogToSd.error("获取日程失败"+throwable.getMessage().toString());
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//    LogToSd.log_init();
//                LogToSd.error("获取日程失败"+throwable.getMessage().toString());
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
//     LogToSd.log_init();
//                LogToSd.error("获取日程失败"+throwable.getMessage().toString());
//            }
//        });
//}


}
